<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            background-color: rgba(21, 6, 240, 0.2);
        }
        .title{
            margin: 50px auto;
            font-size: 40px;
            color: white;
            font-weight: 700;
            text-align: center;
        }
        .f{
            background-color: white;
            width: 300px;
            height: 200px;
            margin: 0 auto;
            border-radius: 10px;
        }
        #i1{
            width: 260px;
            height: 40px;
            margin: 20px 0 0 20px;
        }
        #i2{
            width: 260px;
            height: 40px;
            margin: 20px 0 0 20px;
        }
        button{
            width: 260px;
            height: 40px;
            margin: 20px 0 0 20px;
            background-color: rgb(53, 163, 226);
            color: white;
            text-align: center;
            font-size: 18px;
        }
        p{
            width: 170px;
            margin: 20px 0 0 65px;
            font-size: 12px;
            color: red;
            text-align: center;
        }
    </style>
    <script src="node_modules/jquery/dist/jquery.min.js"></script>
</head>
<body>
    <div class="title">elm后台管理系统</div>
    <div class="f">
        <input type="text" placeholder="用户名" id="i1">
        <input type="password" placeholder="密码" id="i2">
        <button id="btn1">登录</button>
        <p>
            温馨提示：<br>
            未登录过的新用户，自动注册<br>
            注册过的用户可凭账号密码登录
        </p>
    </div>

    <script>
        $("#btn1").click(function () {
            // 获取数据库管理员信息
            let now = new Date();
            let nowt = now.getFullYear() + "-" + (now.getMonth()+1) + "-" + now.getDate() + " " +now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
            let flag = true;
            $.ajax({
                url:"http://127.0.0.1:1818/getUser",
                type:"get",
                success:function (data) {
                    if(Array.isArray(data)){
                        console.log(data)
                        if(data.length!=0) {
                            data.forEach(function (v, i) {
                                if(v.name == $("#i1").val()) {
                                    if(v.pwd == $("#i2").val()) {
                                        window.location.href = "middle.html";
                                        flag = false;
                                        return;
                                    }else {
                                        $(".alert").css("display","block");
                                        flag = false;
                                    }
                                }
                            });
                            if(flag) {
                                $.ajax({
                                    url:"http://127.0.0.1:1818/manager",
                                    type:"post",
                                    data:{
                                        name:$("#i1").val(),
                                        pwd:$("#i2").val(),
                                        date:nowt
                                    },
                                    success:function (data) {
                                        window.location.href = "middle.html";
                                    }
                                });
                            }
                        }else {
                            $.ajax({
                                url:"http://127.0.0.1:1818/manager",
                                type:"post",
                                data:{
                                    name:$("#i1").val(),
                                    pwd:$("#i2").val(),
                                    date:nowt
                                },
                                success:function (data) {
                                    window.location.href = "middle.html";
                                }
                            });
                        }
                    }
                }
            });
        });

    </script>
</body>
</html>